<template>
  <div class="container">
    <div class="search-box">
      <el-input
        v-model="content"
        placeholder="请输入内容"
        class="input-box"
      ></el-input>
      <el-button @click="clear">清除</el-button>
      <el-button type="primary" @click="search">搜索</el-button>
    </div>
    <el-table :data="List" border style="width: 100%">
      <el-table-column prop="id" type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            @click="edit(row.id)"
            size="mini"
            style="margin-right: 10px"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="confirmDel(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :current-page="params._page"
        :page-size="params._limit"
        @current-change="pageChange"
      >
      </el-pagination>
      <el-button size="small" plain @click="openAddDialog">+ 新增</el-button>
    </div>

    <!-- 新增弹框 -->
    <el-dialog
      title="提示"
      :visible="dialogVisible"
      width="30%"
      @close="closeAddDialog"
    >
      <template #default>
        <el-form
          :model="ruleForm"
          :rules="rules"
          status-icon
          ref="ruleForm"
          label-width="100px"
        >
          <el-form-item label="任务名称" prop="taskName">
            <el-input v-model="ruleForm.taskName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="任务描述" prop="taskDetail">
            <el-input
              v-model="ruleForm.taskDetail"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addConfirm">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchList,
  add,
  fetchDetail,
  fetchUpdate,
  fetchDelete,
  fetchSearch,
} from "./api/user";
import { v4 as uuidv4 } from "uuid";
export default {
  name: "MyApp",
  data() {
    return {
      List: [
        {
          id: "",
          name: "",
          des: "",
        },
      ],
      total: 0,
      params: {
        _page: 1,
        _limit: 5,
      },
      dialogVisible: false,
      ruleForm: {
        taskName: "",
        taskDetail: "",
        formId: "",
      },
      rules: {
        taskName: [
          { required: true, message: "请输入任务名称", trigger: "blur" },
        ],
        taskDetail: [
          { required: true, message: "请输入任务描述", trigger: "blur" },
        ],
      },
      content: "",
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      const res = await fetchList(this.params);
      this.List = res.data;
      console.log(this.List);
      this.total = Number(res.headers["x-total-count"]); // 访问对象的属性
    },
    pageChange(page) {
      // console.log(page);
      this.params._page = page;
      this.getList();
    },
    // 编辑
    async edit(id) {
      console.log(id);
      this.dialogVisible = true;
      const { data } = await fetchDetail(id);
      // console.log(data);
      this.ruleForm.taskName = data.name;
      this.ruleForm.taskDetail = data.des;
      this.ruleForm.formId = id;
    },
    // 删除
    async confirmDel(id) {
      await fetchDelete(id);
      this.getList();
    },

    // 点击新增
    openAddDialog() {
      this.dialogVisible = true;
    },

    // 关闭 新增弹窗
    closeAddDialog() {
      this.dialogVisible = false;
      this.$refs.ruleForm.resetFields();
    },
    // 新增确定
    addConfirm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (!this.ruleForm.formId) {
            await add({
              name: this.ruleForm.taskName,
              des: this.ruleForm.taskDetail,
              id: uuidv4(),
            });
          } else {
            await fetchUpdate({
              name: this.ruleForm.taskName,
              des: this.ruleForm.taskDetail,
              id: this.ruleForm.formId,
            });
          }
        } else {
          return false;
        }
        this.$message.success("新增成功"); // 这个多好用，不要忘
        this.getList();
        this.closeAddDialog();
      });
    },
    async search() {
      const { data } = await fetchSearch(this.content);
      console.log(data);
      this.List = data;
    },
    clear() {
      this.content = "";
      this.getList();
    },
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
